package com.example.service;

import com.example.entity.Article;
import com.example.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Service
public class ArticleService {

    @Autowired
    ArticleRepository articleRepository;

    public List<Article> findAll() {
        return articleRepository.findAll();
    }


    @Cacheable("article")
    public Article findArticleById(Integer id) {
        Optional<Article> article = articleRepository.findById(id);
        if (article.isPresent()) return article.get();

        return null;
    }

    public Article updateComment(Integer id, String content) {
        Article article = findArticleById(id);
        article.setContent(content);
        articleRepository.updateArticle(id, content);
        return article;
    }

    public void deleteArticleById(Integer id) {
        articleRepository.deleteById(id);
    }

//    public void addArticle(Integer id,String title,String content){
//        Article article = new Article();
//        article.setId(id);
//        article.setTitle(title);
//        article.setContent(content);
//
//    }

}
